<?php
/*
 * preview.php
 *     Loads a specified page from the Content database, even if it has not been
 *     published. You must specify the page Alias in the 'page' query parameter,
 *     i.e. "preview.php?page=index.html"
 *
 *     You can also pass the 'page' parameter in a POST request.
 *
 *     Invalid content requests return a 404 status page
 *
 */
session_start();
require_once('includes/config.php');
require_once('admin/includes/dbfunctions.php');


// Get 'page' parameter from POST or GET
$pageAlias = trim ((!empty($_POST['page'])) ? $_POST['page'] : $_GET['page'] );

$page = NULL;

// If page was not given in request parameters, use default page
if (strlen($pageAlias) === 0) {
    $pageAlias = $defaultPage;
}

// Look up content in database
$page = getPageInfo($pageAlias);
// If page was found in database, but file does not exist, return error
if ($page !== NULL) {
    if($_GET['revision'] === NULL) {
        $filename = $contentRoot . $page['Filename'];
    } else {
        $filename = $contentRoot . $page['LastFilename'];
    }
    if (!file_exists($filename)) {
        $page = NULL;
    }
    
}

if (!isset($_SESSION['User'])) {
    $page = NULL;
}

if ($page !== NULL) {
    // If content was found, load content
    $TEMPLATE_PAGE_TITLE = $page['Title'];
    $TEMPLATE_PAGE_CONTENT = file_get_contents($filename);
    include($templateFile);
} else {
    // Return 404 error
    header("HTTP/1.x 404 Not Found");
    ?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL <?php echo $_SERVER["REQUEST_URI"]; ?> was not found on this server.</p>
<hr>
<?php echo $_SERVER["SERVER_SIGNATURE"]; ?>
</body></html>
<?
}

?>
